package com.hcrazy.manchester.app.controllers.member;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.MessageSearchForm;
import com.hcrazy.manchester.app.pojo.Message;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.MessageService;
import com.hcrazy.manchester.app.services.UserService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/message")
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addMessage",method=RequestMethod.GET)
	public void addMessage(Model model){
		
	}
	
	/**
	 * 查看信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/viewMessage",method=RequestMethod.GET)
	public void viewMessage(@RequestParam long messageId,Model model){
		try {
			//根据id获取信息内容
			Message message = messageService.getMessage(messageId);
			//更新返回对象
			model.addAttribute("message",message);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	@RequestMapping(value="/recoverMessage",method=RequestMethod.GET)
	public String recoverMessage(@RequestParam long messageId,Model model){
		try {
			//根据id获取信息内容
			messageService.recoverMessage(messageId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/message/message";
	}
	
	/**
	 * 提交信息内容
	 * @param food
	 * @return
	 */
	@RequestMapping(value="/message",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String submitMessage(@ModelAttribute("form") Message message,Model model){
		try {
			message.setFromUserId(0);//管理员
			message.setMessageType(Constant.MESSAGE_TYPE_SYSTEM);
			message.setFromUserName("admin");
			String username = message.getToUserName();
			if(!"0".equals(username)){
				//指定用户名
				User user = userService.getUserByUsername(username);
				if(null == user || user.getUserId() <= 0){
					model.addAttribute("message", "指定用户不存在！");
					return "/admin/failed";
				}else{
					message.setToUserId(user.getUserId());
					
				}
			}else{
				message.setToUserName("全部会员");
			}
			
			messageService.addMessage(message);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/message/message";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/message",method=RequestMethod.GET)
	public void message(Model model,HttpServletRequest req){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllMessages(1, -1);
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", new MessageSearchForm());
		
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search",method=RequestMethod.POST)
	public String searchAddress(@ModelAttribute("form") MessageSearchForm form,
			Model model,HttpServletRequest req){
		//是否执行删除操作
		if(form.getMessageId() > 0){
			try {
				messageService.deleteMessage(form.getMessageId(),form.getFlag());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setFlag(0);
		form.setMessageId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllMessages(form.getPageNum(), form.getMessageType());
		model.addAttribute("pageModel", pageModel);

		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/message/message";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllMessages(int pageNum,int messageType){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		
		try {
			return messageService.getPagedMessages(model, messageType);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
}
